<?php
namespace app\common\lib\redis;

class Predis{
    public $redis="";

    private static $_instance =null;
    public static function getInstance()
    {
        if(empty(self::$_instance)){
            self::$_instance = new self();
        }
       return self::$_instance;
    }
    private function __construct()
    {
        $this->redis=new \Redis();
        $result= $this->redis->connect('127.0.0.1','6379','300');
        if($result === false){
            throw new \Exception('redis connect error');
        }
    }
    public function set($key,$value,$time=0){
        if(!$key){
            return '';
        }
        if(is_array($value)){
            $value =json_encode($value);
        }
        if(!$time){
            return $this->redis->set($key,$value);
        }
        return $this->redis->setex($key,$time,$value);
    }
    public function get($key){

        if(!$key){
            return '';
        }
        return $this->redis->get($key);
    }
    public function sAdd($key,$value)
    {
        return $this->redis->sAdd($key,$value);
    }
    public function sMembers($key)
    {
        return $this->redis->sMembers($key);
    }
    public function sRem($key,$value)
    {
        return $this->redis->sRem($key,$value);
    }
    public function __call($name, $arguments)
    {
        // TODO: Implement __call() method.
        if(count($arguments) != 2) {
            return '';
        }
        return $this->redis->$name($arguments[0],$arguments[1]);
    }
}